package Io.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class BioClient2 {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8081);
             OutputStream outputStream = socket.getOutputStream();
             InputStream inputStream = socket.getInputStream()) {

            // 发送数据到服务器
            String message = "Hello, BIO Server! 我是客户端2号";
            outputStream.write(message.getBytes());

            // 接收服务器响应（阻塞操作）
            byte[] buffer = new byte[1024];
            int bytesRead = inputStream.read(buffer);
            if (bytesRead != -1) {
                String response = new String(buffer, 0, bytesRead);
                System.out.println("Received from server: " + response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
